package com.eastcom.providor.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;

import com.eastcom.bm.atom.Atom_MetalKpiBase;
import com.eastcom.providor.dao.BaseDao;
import com.eastcom.providor.dao.IMetalKpiBaseDao;
import com.eastcom.providor.service.extview.MetalKpiBaseUtil;

public class MetalKpiBaseDao extends BaseDao implements IMetalKpiBaseDao {

	public void editMetalKpiBase(Atom_MetalKpiBase metalKpiBase) throws Exception {
		if(metalKpiBase==null)
			return;
		
		try{
			this.updateObject(metalKpiBase);
		}
		catch(Exception e){
			throw e;
		}
	}
	
	public void editMetalKpiBase(List<Atom_MetalKpiBase> metalKpiBases)throws Exception {
		if(metalKpiBases==null||metalKpiBases.isEmpty())
			return;
		
		try{
			this.saveObjects(metalKpiBases);
		}
		catch(Exception e){
			throw e;
		}

	}

	public Map<Object,Map> getKpiNameById(Collection<String> ids)throws Exception{
		try{
			StringBuffer sql=new StringBuffer("select id,kpi_name_cn from qa_metal_kpi_base_define");
			this.mergeSQLbyFieldLst(sql, "id", ids, true);
			return this.getFieldMapWithKeyFildBySQL(sql.toString(), "id", new String[]{"kpi_name_cn"});
		}
		catch(Exception e){
			throw e;
		}
	}
	
	public Atom_MetalKpiBase findMetalKpiBaseById(String kpiId,boolean initialExpand)throws Exception {
		Session session=null;
		try{
			session=this.getSession();
			Query query=session.createQuery("from Atom_MetalKpiBase where id='"+kpiId+"'");
			query.setCacheable(true);
			List list=query.list();
			Atom_MetalKpiBase metalKpiBase=list==null||list.isEmpty()?null:(Atom_MetalKpiBase)list.get(0);
			if(metalKpiBase!=null&&initialExpand){
				if(!Hibernate.isInitialized(metalKpiBase.getMetalKpiExpands()))
					Hibernate.initialize(metalKpiBase.getMetalKpiExpands());
			}
			else if(metalKpiBase!=null){
				List<Atom_MetalKpiBase> metalKpiBases=new ArrayList<Atom_MetalKpiBase>();
				metalKpiBases.add(metalKpiBase);
				metalKpiBase=MetalKpiBaseUtil.createDatasetDefine(metalKpiBases).get(0);
			}
			
			return metalKpiBase;
		}
		catch(Exception e){
			throw e;
		}
		finally{
			this.releaseSession(session);
		}
	}

	public List<Atom_MetalKpiBase> selectAllKpiBase() throws Exception {
		try{
			return this.getObjects("from Atom_MetalKpiBase");
		}
		catch(Exception e){
			throw e;
		}
	}
	
	public List<Atom_MetalKpiBase> selectMetalKpiBaseByIds(List<String> kpiIds, boolean initialExpand) throws Exception {
		Session session=null;
		try{
			session=this.getSession();
			StringBuffer hql=new StringBuffer("from Atom_MetalKpiBase");
			this.mergeSQLbyFieldLst(hql, "id", kpiIds, true);
			Query query=session.createQuery(hql.toString());
			query.setCacheable(true);
			List<Atom_MetalKpiBase> list=query.list();
			
			if(list!=null&&initialExpand){
				for(Atom_MetalKpiBase kpiBase:list)
				    if(!Hibernate.isInitialized(kpiBase.getMetalKpiExpands()))
					    Hibernate.initialize(kpiBase.getMetalKpiExpands());
			}
			else if(list!=null)
				list=MetalKpiBaseUtil.createDatasetDefine(list);
			
			return list;
		}
		catch(Exception e){
			throw e;
		}
		finally{
			this.releaseSession(session);
		}
	}
	
	public void saveMetalKpiBase(List<Atom_MetalKpiBase> metalKpiBases)throws Exception {
		if(metalKpiBases==null||metalKpiBases.isEmpty())
			return;
		
		try{
			this.saveObjects(metalKpiBases);
		}
		catch(Exception e){
			throw e;
		}
	}
	
	public void deleteMetalKpiBases(List<Atom_MetalKpiBase> metalKpiBases)throws Exception{
		if(metalKpiBases==null||metalKpiBases.isEmpty())
			return;
		
		try{
			this.deleteObjects(metalKpiBases);
		}
		catch(Exception e){
			throw e;
		}
	}

	public List<Atom_MetalKpiBase> selectMetalKpiBaseByBusinessKpiType(String businessType,String kpiType,String name,boolean initialExpand) throws Exception {
		Session session=null;
		
		try{
			session=this.getSession();
			StringBuffer hql=new StringBuffer("from Atom_MetalKpiBase");
			boolean firstCondition=appeandNotNullParam(hql, "businessId", businessType, "%"+businessType+"%", "like", true);
			firstCondition=appeandNotNullParam(hql, "type", kpiType, kpiType, "=", firstCondition);
			appeandNotNullParam(hql, "cn_name", name, "%"+name+"%", "like", firstCondition);
			Query query=session.createQuery(hql.toString());
			query.setCacheable(true);
			List<Atom_MetalKpiBase> metalKpis=query.list();

			if(metalKpis!=null&&initialExpand){
				for(Atom_MetalKpiBase metalKpi:metalKpis){
					if(!Hibernate.isInitialized(metalKpi.getMetalKpiExpands()))
						Hibernate.initialize(metalKpi.getMetalKpiExpands());
				}
			}
			else if(metalKpis!=null)
				metalKpis=MetalKpiBaseUtil.createDatasetDefine(metalKpis);
			
			return metalKpis;
		}
		catch(Exception e){
			throw e;
		}
		finally{
			this.releaseSession(session);
		}
	}

}
